package
{
	import laya.display.Sprite;
	import laya.resource.Texture;

	public class SkillCDMaskSample
	{
		private var _img:Sprite;
		private var _cdMask:Sprite;
		private var _cdSquare:Sprite;
		private var _totalTime:int = 10000;
		private var _leftCdTime:int = _totalTime;
		private var _interval:int = 20;
		private var _counter:int = 0;
		
		public function SkillCDMaskSample()
		{
			
			_img = new Sprite();
			_img.autoSize = true;
			var t:Texture = Laya.loader.getRes("res/item.png");//res/monkey1.png res/item.png
			_img.graphics.drawTexture(t,0,0);
			Laya.stage.addChild(_img);
			_img.pos(60,60,true);
			
			var centerX:Number = _img.width/2;
			var centerY:Number = _img.height/2;
			var w:int = _img.width/2;
			var h:Number = _img.height / 2;
			var r:int = Math.ceil(Math.sqrt(Math.pow(w,2)+Math.pow(h,2)));
					
			_cdSquare = new Sprite();
			Laya.stage.addChild(_cdSquare);
			_cdSquare.x = _img.x;
			_cdSquare.y = _img.y;
			_cdSquare.autoSize = true;
			//通过graphice设置透明度 会影响后面的_cdSquare.graphics对象,可以理解成全局只有一个graphics对象
			_cdSquare.graphics.alpha(0.5); 
			_cdSquare.graphics.drawRect(0,0,_img.width,_img.height,"#ff0000");
			
			_cdMask = new Sprite();
			_cdMask.pos(centerX,centerY); //遮罩对象的坐标系是相当被遮罩对象的 0,0是被遮罩对象的0,0点			
			_cdMask.graphics.drawPie(0,0,50,-89,270,"#00ff00");
			_cdSquare.mask = _cdMask;
			
			Laya.timer.loop(_interval,this,onTimer);
			
		}
		
		private function onTimer():void
		{
			_leftCdTime -= _interval;
						
			var percent:Number = _leftCdTime/_totalTime;
			var sta:Number = 90/360*_totalTime; //需要从负角度开始的耗时
			var costTime:int = _totalTime - _leftCdTime;
			var stAngle:int = 0;
			if(costTime<sta)
			{
				stAngle = -90 + costTime/sta*90;//负角度开始画
			}else
			{
				stAngle = (costTime-sta)/(_totalTime-sta) * 270; //正角度开始画
			}
			_cdMask.graphics.clear();
			_cdMask.graphics.drawPie(0,0,50,stAngle,270,"#00ff00");
			_counter++;
			trace("stAngle=",stAngle,"_leftCdTime=",_leftCdTime,"_counter",_counter);
			if(stAngle>=270)
			{
				Laya.timer.clear(this,onTimer);
			}
			 
		}		
		
	}
}